//
//  MessagePicker.m
//  Guazi
//
//  Created by koolearn on 11-5-12.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MessagePicker.h"


@interface MessagePicker(private)
- (void) displaySMSComposerSheet;
@end



@implementation MessagePicker


@synthesize controller;
@synthesize recipients;
@synthesize body;


- (id) initWithController:(UIViewController*)aController recienpts:(NSArray*)p body:(NSString*)b
{
    self = [super init];
	if ( self ) {
		self.controller = aController;
		self.recipients = p;
		self.body = b;
	}
	return self;
}


- (void) dealloc
{
	[controller release];
	[recipients release];
	[body release];
	[super dealloc];
}


-(void) send  
{
	Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
	if (messageClass != nil) {
		// Check whether the current device is configured for sending SMS messages
		if ([messageClass canSendText]) {
			[self displaySMSComposerSheet];
		}
		else {
			UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"设备没有短信功能" 
															message:@"您的设备不能发送短信" 
														   delegate:nil 
												  cancelButtonTitle:@"确定" 
												  otherButtonTitles:nil];
			[alert show];
			[alert release];
		}
	}
	else {
		UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"iOS版本过低" 
														message:@"iOS版本过低,iOS4.0以上才支持程序内发送短信" 
													   delegate:nil 
											  cancelButtonTitle:@"确定" 
											  otherButtonTitles:nil];
		[alert show];
		[alert release];
	}
}


-(void)displaySMSComposerSheet
{
	MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
	picker.messageComposeDelegate = self;
	picker.recipients = self.recipients;
	picker.body = self.body;
    picker.title = @"发送短信";

	[controller.navigationController presentModalViewController:picker animated:YES];
	[picker release];
}


- (void)messageComposeViewController:(MFMessageComposeViewController *)messageComposeViewController
				 didFinishWithResult:(MessageComposeResult)result {
	switch (result)
	{
		case MessageComposeResultCancelled:
			NSLog(@"Result: SMS sending canceled");
			break;
		case MessageComposeResultSent:
			NSLog(@"Result: SMS sent");
			break;
		case MessageComposeResultFailed:
			NSLog(@"Result: sms fail");
			UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"短信发送失败" 
															message:@"短信发送失败" 
														   delegate:nil 
												  cancelButtonTitle:@"确定" 
												  otherButtonTitles:nil];
			[alert show];
			[alert release];
			break;
		default:
			NSLog(@"Result: SMS not sent");
			break;
	}
    
    [controller.navigationController dismissModalViewControllerAnimated:YES];
}

@end
